%% Plot change regulatory discount curve
% Kristy A.E. Jansen

clear all; clc; format longG;

% B2_ChangeRegulatoryCurve.m
% This do-file computes value of liabilities using aggregate pension
% payments at every horizon and the yield curves with and without UFR
% Cash Flows: end of 2011 values (annual)
% Output: Table IA11, Figures 2 and IA2

%% Load CF data and yield curves
CF=readmatrix('Data/total_cashflows.xlsx');
CF=CF(:,2:end)/1000;

yields=readmatrix('Data/yield_curve_UFR.xlsx','Sheet','Sheet1');
y=yields(1:length(CF),2); y_min100bp=yields(1:length(CF),4);
y_UFR=yields(1:length(CF),3); y_UFR_min100bp=yields(1:length(CF),5);
y_20y=yields(1:length(CF),6); y_UFR_20y=yields(1:length(CF),7);

T=length(y);


%% Compute DCF
for t=1:T
DF(t,1)=1./(1+y(t,1))^t;
DF_UFR(t,1)=1./(1+y_UFR(t,1))^t;
DF_min100bp(t,1)=1./(1+y_min100bp(t,1))^t;
DF_UFR_min100bp(t,1)=1./(1+y_UFR_min100bp(t,1))^t;
DF_20y(t,1)=1./(1+y_20y(t,1))^t;
DF_UFR_20y(t,1)=1./(1+y_UFR_20y(t,1))^t;
end

DCF=CF.*(DF)';
DCF_UFR=CF.*(DF_UFR)';
DCF_min100bp=CF.*(DF_min100bp)';
DCF_UFR_min100bp=CF.*(DF_UFR_min100bp)';
DCF_20y=CF.*(DF_20y)';
DCF_UFR_20y=CF.*(DF_UFR_20y)';

%% Compute value liabilities 
liab=sum(DCF); liab_20=sum(DCF(:,21:end));
liab_UFR=sum(DCF_UFR,2); liab_UFR_20=sum(DCF_UFR(:,21:end));
liab_min100bp=sum(DCF_min100bp); liab_20_min100bp=sum(DCF_min100bp(:,21:end)); 
liab_UFR_min100bp=sum(DCF_UFR_min100bp); liab_UFR_20_min100bp=sum(DCF_UFR_min100bp(:,21:end)); 
liab_20y=sum(DCF_20y); liab_20_20y=sum(DCF_20y(:,21:end)); 
liab_UFR_20y=sum(DCF_UFR_20y);  liab_UFR_20_20y=sum(DCF_UFR_20y(:,21:end));


%% Table IA11: Economic versus regulatory value of the liabilities

% parallel shift IRS
% all maturities
A = [liab liab_UFR 100*(liab_UFR-liab)/liab;];
B = [liab_min100bp liab_UFR_min100bp 100*(liab_UFR_min100bp-liab_min100bp)/liab_min100bp];
C = [B(1)-A(1) B(2)-A(2) 100*((B(2)-A(2))-(B(1)-A(1)))/(B(1)-A(1))];
round([A;B;C],2)

%maturities longer than 20 years
D = [liab_20 liab_UFR_20 100*(liab_UFR_20-liab_20)/liab_20];
E = [liab_20_min100bp liab_UFR_20_min100bp 100*(liab_UFR_20_min100bp-liab_20_min100bp)/liab_20_min100bp];
F = [E(1)-D(1) E(2)-D(2) 100*((E(2)-D(2))-(E(1)-D(1)))/(E(1)-D(1))];
round([D;E;F],2)

% Shock 20 year
% all maturities
A = [liab liab_UFR 100*(liab_UFR-liab)/liab];
B = [liab_20y liab_UFR_20y 100*(liab_UFR_20y-liab_20y)/liab_20y];
C = [B(1)-A(1) B(2)-A(2) 100*((B(2)-A(2))-(B(1)-A(1)))/(B(1)-A(1))];
round([A;B;C],2)

%maturities longer than 20 years
D = [liab_20 liab_UFR_20 100*(liab_UFR_20-liab_20)/liab_20];
E = [liab_20_20y liab_UFR_20_20y 100*(liab_UFR_20_20y-liab_20_20y)/liab_20_20y];
F = [E(1)-D(1) E(2)-D(2) 100*((E(2)-D(2))-(E(1)-D(1)))/(E(1)-D(1))];
round([D;E;F],2)

%% Figures 2: Change in the regulatory discount curve 

x=figure;
plot(1:T,y*100,'r',1:T,y_min100bp*100,'b',1:T,y_UFR*100,'g--',1:T,y_UFR_min100bp*100,'k.','MarkerSize',9,'LineWidth',4) 
legend('Economic discount curve','Economic discount curve parallel shift','Regulatory discount curve','Regulatory discount curve parallel shift','Location','southeast','FontSize',14,'fontname','Bookman Old Style')
xlabel('Maturity','FontSize',14,'fontname','Bookman Old Style')
ylabel('Yield','FontSize',14,'fontname','Bookman Old Style')
grid on

x=figure;
plot(1:T,y*100,'r',1:T,y_20y*100,'b--',1:T,y_UFR*100,'g',1:T,y_UFR_20y*100,'k.','MarkerSize',9,'LineWidth',4) 
legend('Economic discount curve','Economic discount curve parallel shift','Regulatory discount curve','Regulatory discount curve parallel shift','Location','southeast','FontSize',14,'fontname','Bookman Old Style')
xlabel('Maturity','FontSize',14,'fontname','Bookman Old Style')
ylabel('Yield','FontSize',14,'fontname','Bookman Old Style')
grid on

%% Figure IA2: Cash flow distribution of the liabilities

x=figure;
plot(1:T,CF,'r-.',1:T,DCF,'b','MarkerSize',9,'LineWidth',4) 
legend('Cash flows','Discounted Cash Flows','Location','northeast','FontSize',14,'fontname','Bookman Old Style')
xlabel('Maturity','FontSize',14,'fontname','Bookman Old Style')
ylabel('Cash flows','FontSize',14,'fontname','Bookman Old Style')
grid on
